package com.wangxinyuchuan.demo.singleton;

/**
 * 静态内部类方式
 * JVM保证单例
 * 加载外部类时不会加载内部类，这样可以实现懒加载
 */
public class Demo7 {

    private Demo7() {

    }

    private static class Demo7Inner {
        private final static Demo7 INSTANCE = new Demo7();
    }

    public static Demo7 getInstance() {
        return Demo7Inner.INSTANCE;
    }


    public static void main(String[] args) {
        Demo7 a = Demo7.getInstance();
        Demo7 b = Demo7.getInstance();
        System.out.println(a == b);


        // 模拟多线程获取单例对象
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Demo7.getInstance());
            }).start();
        }
    }
}
